package org.example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync  // 启用异步任务执行
public class AsyncConfig {

    @Bean(name = "myExecutor")
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);  // 设置核心线程数
        executor.setMaxPoolSize(50);  // 设置最大线程数
        executor.setQueueCapacity(100);  // 设置队列容量
        executor.setThreadNamePrefix("async-executor-");  // 线程名前缀
        executor.initialize();
        return executor;
    }
}
